/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.language;

import java.io.IOException;
import java.util.LinkedList;

import ac.jp.u_tokyo.SyncLib.dictionaries.KeyFactory;
import ac.jp.u_tokyo.SyncLib.language.factories.DictMapCombinatorFactory;

public class DictMapBody extends SyncBody
{
	public DictMapBody(int line)
	{
		super(line);
	}

	SyncBody _inner;
	LinkedList<Const> _factoryList = new LinkedList<Const>();
	
	public void setInner(SyncBody inner)
	{
		_inner = inner;
	}
	
	public void addFactory(Const c) {
		_factoryList.add(c);
	}

	@Override
	public int getParaCount() throws EvaluationFailedException
	{
		return _inner.getParaCount();
	}
	
	protected String getFactoryName() {
		return DictMapCombinatorFactory.class.getName();
	}
	static String getKeyFactoryName() {
		return KeyFactory.class.getName().replace('$', '.');
	}
	
	@Override
	public String writeInitializeFactory(Appendable sb,
			VarNameGenerator generator, ConstMapper constMap)
			throws EvaluationFailedException, IOException
	{
		String var = generator.getNextName();
		String inner = _inner.writeInitializeFactory(sb, generator, constMap);
		sb.append(getFactoryName()).append(" ").append(var).append(" = new ").append(getFactoryName()).append("(");
		sb.append(" new ").append(getKeyFactoryName()).append("[]{");
		for(Const c : _factoryList) {
			c.write(sb, constMap);
			sb.append(", ");
		}
		sb.append("}, ").append(inner);
		sb.append(",").append(new Integer(getParaCount() - _factoryList.size()).toString());
		sb.append(");\n");;
		
		return var;
	}

}
